using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Administration;

namespace JobCreateInDocs3.Features.JobCreateInDocs3
{

    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("ed3d27d4-eece-4f25-9bef-581a0e94abdd")]
    public class JobCreateInDocs3EventReceiver : SPFeatureReceiver
    {

        #region Members

        string siteUrl = @"http://localhost/sites/shop";
        string srvName = @"S-WSS02";

        #endregion

        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            ////SPSecurity.RunWithElevatedPrivileges(delegate()
            ////{
            ////    using (SPSite site = properties.Feature.Parent as SPSite)
            ////    {
            
            //// delete preview job item
            ////SPSite site = properties.Feature.Parent as SPSite;

            //SPWebApplication webApp = SPWebApplication.Lookup(new Uri(siteUrl));
            //SPServer srv = new SPServer(srvName);
            //foreach (SPJobDefinition job in webApp.Sites.WebApplication.JobDefinitions)
            //{
            //    if (
            //        job.Name == "JobCreateInDocs_v3_1"
            //        || job.Name == "JobCreateInDocs_v3_2"
            //        || job.Name == "JobCreateInDocs_v3_3"
            //        || job.Name == "JobCreateInDocs_v3_4"
            //        )
            //    {
            //        job.Delete();
            //    }
            //}

            ////JobCreateInDocs myTJ = new JobCreateInDocs(@"JobCreateInDocs_v3_4", webApp, srv, SPJobLockType.None);
            //JobCreateInDocs myTJ = new JobCreateInDocs(@"JobCreateInDocs_v3_4", webApp);

            //SPMinuteSchedule schedule = new SPMinuteSchedule();
            //schedule.BeginSecond = 0;
            //schedule.EndSecond = 59;
            //schedule.Interval = 1;
            ////SPSchedule schedule = SPSchedule.FromString("every 20 minutes between 7:00:00 and 23:00:00");
            //myTJ.Schedule = schedule;
            //myTJ.Update();

            ////    }
            ////});
        }
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            ////SPSecurity.RunWithElevatedPrivileges(delegate()
            ////{
            ////    using (SPSite site = properties.Feature.Parent as SPSite)
            ////    {
            ////SPSite site = properties.Feature.Parent as SPSite;
            //SPWebApplication webApp = SPWebApplication.Lookup(new Uri(siteUrl));
            //SPServer srv = new SPServer(srvName);
            //foreach (SPJobDefinition job in webApp.Sites.WebApplication.JobDefinitions)
            //{
            //    if (
            //        job.Name == "JobCreateInDocs_v3_1"
            //        || job.Name == "JobCreateInDocs_v3_2"
            //        || job.Name == "JobCreateInDocs_v3_3"
            //        || job.Name == "JobCreateInDocs_v3_4"
            //        )
            //    {
            //        job.Delete();
            //    }
            //}
            ////    }
            ////});
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
        }


        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
        }

        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}

    }
}
